<?php
//https://blog.csdn.net/qq_40770272/article/details/102702620   解决 跨域 post
//  指定允许其他域名访问
//json头
header("Content-type: application/json");
//跨域
header("Access-Control-Allow-Credentials: true");
header("Access-Control-Allow-Origin: *");
//CORS
header("Access-Control-Request-Methods:GET, POST, PUT, DELETE, OPTIONS");
header('Access-Control-Allow-Headers:x-requested-with,content-type,test-token,test-sessid');//注意头部自定义参数不要用下划线

require_once 'Db.php';

$_postData = json_decode($_POST['postData']);

function addOrder($_postData){

    $db = new Db();
    $my_db = $db->getDB();
    $supplier_id = 67;
    $order_code = 'D'.time();
    $order_sql = "INSERT INTO bd_order(
        `supplier_id`,
        `order_code`
        )VALUES(
            '$supplier_id',
            '$order_code'
        )";
        
    $res = $my_db->query($order_sql);
    if($res){
        $detail_sql = '';
        $order_id = mysqli_insert_id($my_db);
        foreach($_postData as $_key => $_value){
            $detail_sql .= "INSERT INTO bd_order_detail (
                `good_id`,
                `order_id`,
                `pr_name`,
                `pr_num`
            )VALUES(
                '{$_value->id}',
                '{$order_id}',
                '{$_value->trade_name}',
                '{$_value->count}'
            );";
        }
        $r = $my_db->multi_query($detail_sql);
        if($r){
            return json_encode(['status' => 1,'msg'=>'结账成功']);
        }else{
            return json_encode(['status' => 0,'msg' => '结账失败']);
        }
    }
}

$res = addOrder($_postData);
echo $res;

